package com.inspur.gs.sbssdl.sbwhgl.gyjgl.gyjgl.back.bizactions;
import com.google.common.collect.ImmutableMap;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class gyjghsqghWriteBackMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String dataid;
	private String jlz;
	public gyjghsqghWriteBackMgrAction(IBEManagerContext managerContext, String dataid, String jlz) {
		super(managerContext);
		this.dataid = dataid;
		this.jlz = jlz;
	}
	@Override
	public  void execute() {
		if (jlz.equals("Back")){
			return;
		}
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			//点检配置变更
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			//归还id
			String gyjghid = "4939bfe5-a636-432e-bc91-65de73b8b1ee";
			IStandardLcp gyjghlcp = iLcpFactory.createLcpByBEId(gyjghid);
			//台账id
			String gyjtzid = "46911fe3-0d25-4e16-9e9e-c93393525ced";
			IStandardLcp gyjtzlcp = iLcpFactory.createLcpByBEId(gyjtzid);

			IEntityDataCollection lymx = gyjghlcp.retrieve(dataid).getData().getChilds().get("ghmx");

			Map<String, List<String>> tzidGroupedByLx = lymx.stream()
					.filter(s -> ((IValuesContainer)s.getValue("tzid")).getValue("tzid").toString() != null && s.getValue("ghlx") != null)
					.collect(Collectors.groupingBy(
							s -> (String) s.getValue("ghlx"),  // 分组字段：lx
							Collectors.mapping(
									s -> {
										return ((IValuesContainer)s.getValue("tzid")).getValue("tzid").toString();
									},
									Collectors.toList()
							)
					));
			// lx → zt 状态映射表
			Map<String, String> STATUS_MAP = ImmutableMap.<String, String>builder()
					.put("0", "1")
					.put("1", "2")
					.build();

			tzidGroupedByLx.forEach((lx, tzidList) -> {
				String zt = STATUS_MAP.get(lx);
				if (zt != null) {
					tzidList.forEach(tzid -> {
						ModifyChangeDetail change = new ModifyChangeDetail(tzid);
						change.getPropertyChanges().put("zt", zt);
						gyjtzlcp.modify(change);
					});
				}
			});
			gyjtzlcp.save();
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}
}